package aps.module_Machines;

//Java
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;

import buildcraft.core.network.PacketPayload;
import buildcraft.core.proxy.CoreProxy;

//Minecraft
import net.minecraft.src.Block;
import net.minecraft.src.Container;
import net.minecraft.src.GuiButton;
import net.minecraft.src.GuiContainer;
import net.minecraft.src.GuiScreen;
import net.minecraft.src.InventoryPlayer;
import net.minecraft.src.Item;

//Forge
import net.minecraftforge.client.MinecraftForgeClient;

//APS
import aps.GuiAPSSlider;
import aps.module_Core.APSPacketIDs;
import aps.module_Core.APSPacketUpdate;
import aps.module_Core.ContainerAPS;
import aps.module_Machines.TileEntityEnergyStore.UpdateMessage;
import aps.BuildcraftAPS;


public class GuiEnergyStore extends GuiContainer
{
	private TileEntityEnergyStore storeInventory;
	
	GuiAPSSlider InSlider;
	GuiAPSSlider OutSlider;
	
	float InSlider_old = -1;
	float OutSlider_old = -1;
	
	
	String guiFile = BuildcraftAPS.imageFilesRoot + "EnergyStoreGUI.png";
	
	public GuiEnergyStore(TileEntityEnergyStore store, InventoryPlayer playerInv)
	{
		super(new ContainerEnergyStore(store, playerInv));
		storeInventory = store;
	}
	
	@Override
	public void initGui()
	{
		super.initGui();
		
		int topLeftX = (width - xSize) / 2;
		int topLeftY = (height - ySize) / 2;
		
		//(int id, int xPos, int yPos, int width, int height, int notchTexXPos, int notchTexYPos, int notchTexWidth, int notchTexHeight, String notchTex, String label, float startingValue)
		InSlider = new GuiAPSSlider(420, topLeftX + 87, topLeftY + 40, 71, 8, 184, 0, 5, 7, guiFile, "", storeInventory.getScaledPowerMax(false, 1));
		OutSlider = new GuiAPSSlider(421, topLeftX + 87, topLeftY + 53, 71, 8, 184, 0, 5, 7, guiFile, "", storeInventory.getScaledPowerMax(true, 1));
		
		controlList.add(InSlider);
		controlList.add(OutSlider);
	}
	
	@Override
	protected void mouseMovedOrUp(int posX, int posY, int event)
	{
		if (event == 0)  //mouse up event
		{
			for (int element = 0; element < this.controlList.size(); element++) //get all elements in the container
			{
				if (this.controlList.get(element) instanceof GuiAPSSlider) 
				{
					((GuiAPSSlider)this.controlList.get(element)).dragging = false;
				}
			}
		}
	}
	
	protected void drawGuiContainerForegroundLayer(int x, int y)
    {
        fontRenderer.drawString("Inventory", 8, (ySize - 96) + 2, 0x404040);
        
        String s1 = "" + storeInventory.UserMaxPowerIn;
		fontRenderer.drawString(s1, 157 - fontRenderer.getStringWidth(s1), 40, 0x404040);
		String s2 = "" + storeInventory.UserMaxPowerOut;
		fontRenderer.drawString(s2, 157 - fontRenderer.getStringWidth(s2), 53, 0x404040);
        
    }
	
	protected void drawGuiContainerBackgroundLayer(float f, int i , int j)
    {
        int guiTex = mc.renderEngine.getTexture(guiFile);
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        int topLeftX = (width - xSize) / 2;
        int topLeftY = (height - ySize) / 2;
        mc.renderEngine.bindTexture(guiTex);
        drawTexturedModalRect(topLeftX, topLeftY, 0, 0, xSize, ySize);
        
        if (storeInventory.isDraining())
        	drawTexturedModalRect(topLeftX + 87, topLeftY + 16, 176, 0, 8, 8); //Draining indicator
        
        displayStoreGauge(topLeftX, topLeftY, 68, 14, (int) storeInventory.getScaledPowerStored(58)); //Power Gauge -- Seph - renamed, displayTempGauge didn't make sense
        
        displayPowerGauge(topLeftX, topLeftY, 87, 40, (int) storeInventory.getScaledPower(false, 71)); //Power Input Gauge
        
        displayPowerGauge(topLeftX, topLeftY, 87, 53, (int) storeInventory.getScaledPower(true,  71)); //Power Output Gauge
        
        storeInventory.UserMaxPowerIn = (int) InSlider.getScaledSliderValue(storeInventory.MaxPowerIn);
        storeInventory.UserMaxPowerOut = (int) OutSlider.getScaledSliderValue(storeInventory.MaxPowerOut);
    }
	
	private void displayStoreGauge(int TopLeftX, int TopLeftY, int GaugeTopLeftX, int GaugeTopLeftY, int BarHeight)
	{
		int start = 0;

		while (true)
		{
			int x = 0;

			if (BarHeight > 8) {
				x = 8;
				BarHeight -= 8;
			} else {
				x = BarHeight;
				BarHeight = 0;
			}

			drawTexturedModalRect(TopLeftX + GaugeTopLeftX, TopLeftY + GaugeTopLeftY + 58 - x - start, 176, 8 + (8 - x), 8, x); //The 58 is the gauge height
			start = start + 8;

			if (x == 0 || BarHeight == 0) {
				break;
			}
		} //This while draws the repeating gauge graphic.
    }
	
	private void displayPowerGauge(int TopLeftX, int TopLeftY, int GaugeTopLeftX, int GaugeTopLeftY, int BarWidth)
	{
		MinecraftForgeClient.preloadTexture(guiFile);
		
		int start = 0;

		while (true)
		{
			int x = 0;

			if (BarWidth > 8) {
				x = 8;
				BarWidth -= 8;
			} else {
				x = BarWidth;
				BarWidth = 0;
			}

			drawTexturedModalRect(TopLeftX + GaugeTopLeftX + start, TopLeftY + GaugeTopLeftY, 176, 8, x, 8); //The 58 is the gauge height
			start = start + 8;

			if (x == 0 || BarWidth == 0) {
				break;
			}
		} //This while draws the repeating gauge graphic.
    }
	
	public void updateScreen()
	{
		super.updateScreen();
		
		this.InSlider.sliderValue = storeInventory.getScaledPowerMax(false, 1);
		this.OutSlider.sliderValue = storeInventory.getScaledPowerMax(true, 1);
		
		if (storeInventory.getScaledPowerMax(false, 1) != this.InSlider_old ||
			storeInventory.getScaledPowerMax(true, 1) != this.OutSlider_old
			)
		{
			this.InSlider_old = storeInventory.getScaledPowerMax(false, 1);
			this.OutSlider_old = storeInventory.getScaledPowerMax(true, 1);
			
			sendUpdatePacket();
		}
	}
	
	public void sendUpdatePacket()
	{		
		UpdateMessage message = new UpdateMessage();

		message.maxIn = storeInventory.UserMaxPowerIn;
		message.maxOut = storeInventory.UserMaxPowerOut;
		
		//Container conatiner = (Container)this.inventorySlots;
		
		if (CoreProxy.proxy.isRenderWorld(storeInventory.worldObj))
		{
			PacketPayload payload = TileEntityEnergyStore.updateMessageWrapper.toPayload(storeInventory.xCoord, storeInventory.yCoord, storeInventory.zCoord, message);
			
			APSPacketUpdate packet = new APSPacketUpdate(APSPacketIDs.UPDATE_ENERGYSTORE, payload);
			packet.posX = storeInventory.xCoord;
			packet.posY = storeInventory.yCoord;
			packet.posZ = storeInventory.zCoord;
			
			CoreProxy.proxy.sendToServer(packet.getPacket());
		}
	}
}